<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>批量添加</title>
    <%! ResultSet rs;%>
    <%
        String dbName = "student";
        String userName = "root";
        String userPasswd = "123456";
        String url = "jdbc:mysql://172.21.32.100/" + dbName + "?user=" + userName + "&password=" + userPasswd+"&useUnicode=true&characterEncoding=utf-8";
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection conn = DriverManager.getConnection(url);
        String[] stuNos = new String[]{"202203010002","202203010003","202203010004","202203010005"};
        String[] stuNames = new String[]{"赵博","王松","张蕾","王哲"};
        String[] stuSexs = new String[]{"男","男","女","男"};
        String[] stuPoliticalStatus = new String[]{"预备党员","共青团员","预备党员","共青团员"};
        String[] stuBirthdays = new String[]{"2002-11-01","2002-11-02","2002-11-03","2002-11-04"};
        String[] stuIdentitCards = new String[]{"120100200211012711","120100200211022711","120100200211032711","120100200211042711"};
        String[] stuSpecialitys = new String[]{"软件技术","视觉传达","信息安全","机械制造"};
        String[] stuAddress = new String[]{"天津市河西区","天津市和平区","天津市南开区","天津市河北区"};
        String[] stuPostcode = new String[]{"300001","300002","300003","300004"};
        String[] stuTelephones = new String[]{"18739077658","19739077658","18735077658","18739037658"};
        String[] stuEmails = new String[]{"18739077658@163.com","19739077658@163.com","18735077658@163.com","18739037658@163.com"};
        String sql = "INSERT into student(stu_no,stu_name,stu_sex,stu_politicalstatus,stu_birthday,stu_identitycard,stu_speciality,stu_address,stu_postcode,stu_telephone,stu_email)VALUES(?,?,?,?,?,?,?,?,?,?,?)";
        PreparedStatement prepardStatement = conn.prepareStatement(sql);
        for (int i = 0; i<4;i++){
            prepardStatement.setString(1,stuNos[i]);
            prepardStatement.setString(2,stuNames[i]);
            prepardStatement.setString(3,stuSexs[i]);
            prepardStatement.setString(4,stuPoliticalStatus[i]);
            prepardStatement.setString(5,stuBirthdays[i]);
            prepardStatement.setString(6,stuIdentitCards[i]);
            prepardStatement.setString(7,stuSpecialitys[i]);
            prepardStatement.setString(8,stuAddress[i]);
            prepardStatement.setString(9,stuPostcode[i]);
            prepardStatement.setString(10,stuTelephones[i]);
            prepardStatement.setString(11,stuEmails[i]);
            prepardStatement.addBatch();
        }
        int [] result = prepardStatement.executeBatch();
        if (result.length>0)
            System.out.print("<script type='text/javascript'>alert(添加失败);</script>");
        sql = "select * from student";
        prepardStatement = conn.prepareStatement(sql);
        rs = prepardStatement.executeQuery();
    %>
</head>
<body>
<table border="1">
    <th colspan="7" style=" font-size: x-large; alignment: center">学生信息表</th>
    <tr style="text-align: center">
        <td>学号</td>
        <td>姓名</td>
        <td>性别</td>
        <td>政治身份</td>
        <td>出生日期</td>
        <td>身份证号</td>
        <td>所在系部</td>
    </tr>
    <% while (rs.next()) { %>
    <tr style="text-align:center">
        <td><%=rs.getString(1)%></td>
        <td><%=rs.getString(2)%></td>
        <td><%=rs.getString(3)%></td>
        <td><%=rs.getString("stu_politicalstatus")%></td>
        <td><%=rs.getString("stu_birthday")%></td>
        <td><%=rs.getString("stu_identitycard")%></td>
        <td><%=rs.getString("stu_speciality")%></td>
    </tr>
    <% } %>
    <%
        rs.close();
        prepardStatement.close();
        conn.close();
    %>
</table>
</body>
</html>
